// ProcessUtils.cpp
#include "utils/ProcessUtils.h"

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <limits.h>
#endif

namespace tower {
namespace utils {

std::string getCurrentProcessName() {
#ifdef _WIN32
    char buffer[MAX_PATH];
    GetModuleFileNameA(NULL, buffer, MAX_PATH);
    std::string::size_type pos = std::string(buffer).find_last_of("\\/");
    return std::string(buffer).substr(pos + 1);
#else
    char buffer[PATH_MAX];
    ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1);
    if (len != -1) {
        buffer[len] = '\0';
        std::string::size_type pos = std::string(buffer).find_last_of("/");
        return std::string(buffer).substr(pos + 1);
    } else {
        // Fallback for macOS and other Unix-like systems
        pid_t pid = getpid();
        sprintf(buffer, "/proc/%d/comm", pid);
        FILE* file = fopen(buffer, "r");
        if (file) {
            fgets(buffer, sizeof(buffer), file);
            fclose(file);
            return std::string(buffer);
        }
    }
    return "";
#endif
}

} // namespace utils
} // namespace tower
